﻿using UnityEngine;
using ZCTouchDrawBoard;
using ZCUtil.Singleton;
using ZCUIManager;
using UnityEngine.EventSystems;

namespace ZCMain
{
    /// <summary>
    /// 选择不同的组件, 做不同响应的Controller
    /// </summary>
    public class MainController : Singleton<MainController>
    {
        #region PublicField
        /// <summary>
        /// 主界面选中框
        /// </summary>
        private MainSelectView m_SelectView;

        /// <summary>
        /// 主界面笔
        /// </summary>
        private MainPaintView m_PaintView;

        /// <summary>
        /// 放大镜
        /// </summary>
        private MainZoomView m_ZoomView;

        /// <summary>
        /// 是否编辑状态
        /// </summary>
        private bool m_Editing;

        /// <summary>
        /// 主界面
        /// </summary>
        private MainView m_MainView;

        /// <summary>
        /// Model
        /// </summary>
        private MainModel m_MainModel;
        #endregion

        #region Public Method
        public bool Editing => m_Editing;
        #endregion

        #region LiveTime
        public void Initialize()
        {
            #region
            m_MainView = UIManager.Instance.GetUIView<MainView>(eUIType.MainView);
            m_SelectView = UIManager.Instance.GetUIView<MainSelectView>(eUIType.MainSelectView);
            m_PaintView = UIManager.Instance.GetUIView<MainPaintView>(eUIType.MainPaintView);
            m_ZoomView = UIManager.Instance.GetUIView<MainZoomView>(eUIType.MainZoomView);
            m_MainModel = new MainModel();
            m_MainModel.Initialize();
            #endregion

            #region TouchDrawBoardSystem
            InitializeTouchDrawingSystem();
            #endregion
        }

        public void Tick()
        {

        }

        public void Uninitialize()
        {
            #region TouchDrawBoardSystem
            UninitializeTouchDrawingSystem();
            #endregion

            #region 选中框
            m_SelectView.Uninitialize();
            m_MainView.Uninitialize();
            m_MainModel.Uninitialize();
            #endregion
        }
        #endregion

        #region PublicMethod
        /// <summary>
        /// 更新选中的view
        /// </summary>
        /// <param name="selectGo"></param>
        public void UpdateSelectViewAction(GameObject selectGo)
        {
            m_SelectView.SelectGo = selectGo;
            if (!m_Editing)
            {
                TouchDrawingController.Instance.UpdateSelectGo(selectGo);
            }
        }

        /// <summary>
        /// 设置视图的显隐
        /// </summary>
        /// <param name="enable"></param>
        public void SetActiveViewAction(bool enable)
        {
            if (enable)
            {
                m_MainView.Show();
            }
            else
            {
                m_MainView.Hide();
            }
        }

        /// <summary>
        /// 切换视图
        /// </summary>
        /// <param name="uiType"></param>
        public bool SwitchView(eUIType uiType)
        {
            if (m_Editing)
            {
                Logger.i($"not switch view is {uiType}.");
                return false;
            }
            m_PaintView.Hide();
            SetActiveViewAction(false);
            TouchDrawingController.Instance.SetActiveViewAction(false);
            switch (uiType)
            {
                case eUIType.MainView:
                    SetActiveViewAction(true);
                    break;
                case eUIType.TouchDrawingView:
                    TouchDrawingController.Instance.SetActiveViewAction(true);
                    break;
                case eUIType.AnimationEffectView:
                    break;
                case eUIType.PostEffectView:
                    break;
                case eUIType.MoreView:
                    break;
                default:
                    SetActiveViewAction(true);
                    break;
            }
            Logger.i($"switch view type is {uiType}.");
            return true;
        }
        #endregion

        #region PrivateMethod
        public void InitializeTouchDrawingSystem()
        {
            TouchDrawingController.Instance.Initialize();
            TouchDrawingController.Instance.SetCreatePaperCallback(CreatePaperCallback);
            TouchDrawingController.Instance.SetClickPaperCallback(ClickPaperCallback);
            TouchDrawingController.Instance.SetClickDownPaperCallback(ClickDownPaperCallback);
            TouchDrawingController.Instance.SetClickUpPaperCallback(ClickUpPaperCallback);
            TouchDrawingController.Instance.SetDragPaperCallback(DragPaperCallback);
            TouchDrawingController.Instance.SetEditPaperCallback(EditPaperCallback);
            TouchDrawingController.Instance.SetPaintSizeChangeCallback(PaintSizeChangeCallback);
            TouchDrawingController.Instance.SetActiveViewAction(false);
            TouchDrawingController.Instance.DoublePaper = ProgramManager.mainCanvasRender.DoublePaper;
        }

        public void UninitializeTouchDrawingSystem()
        {
            TouchDrawingController.Instance.Uninitialize();
        }

        public void CreatePaperCallback(GameObject paperGo)
        {
            SelectAttribute attribute = paperGo.AddComponent<SelectAttribute>();
            attribute.SelectType = eSelectType.Paper;

            UpdateSelectViewAction(paperGo);
        }

        public void ClickPaperCallback(GameObject clickGo, PointerEventData eventData)
        {
            UpdateSelectViewAction(clickGo);
        }

        public void ClickDownPaperCallback(GameObject clickGo, PointerEventData eventData)
        {
            if (m_Editing)
            {
                m_PaintView.SetPoint(eventData.position);
                m_PaintView.Show();
                m_ZoomView.SetPoint(eventData.position);
                m_ZoomView.Show();
            }
        }

        public void ClickUpPaperCallback(GameObject clickGo, PointerEventData eventData)
        {
            m_PaintView.SetPoint(ProgramManager.mainCanvasRectTransform.position);
            m_PaintView.Hide();
            m_ZoomView.Hide();
        }

        public void DragPaperCallback(GameObject clickGo, PointerEventData eventData, bool editing)
        {
            if (m_Editing)
            {
                m_PaintView.SetPoint(eventData.position);
                m_PaintView.Show();
                m_ZoomView.SetPoint(eventData.position);
                m_ZoomView.Show();
            }
            else
            {
                if (m_SelectView.SelectGo == clickGo)
                {
                    RectTransform mainCanvasRectTransform = ProgramManager.mainCanvasRectTransform;

                    RectTransform transform = clickGo.GetComponent<RectTransform>();
                    Vector2 scale = mainCanvasRectTransform.localScale;
                    Vector2 targetPosition = transform.anchoredPosition + (eventData.delta / scale);
                    if (targetPosition.x > mainCanvasRectTransform.rect.xMax / scale.x || targetPosition.x < mainCanvasRectTransform.rect.xMin / scale.x)
                    {
                        targetPosition.x = transform.anchoredPosition.x;
                    }
                    if (targetPosition.y > mainCanvasRectTransform.rect.yMax / scale.y || targetPosition.y < mainCanvasRectTransform.rect.yMin / scale.y)
                    {
                        targetPosition.y = transform.anchoredPosition.y;
                    }
                    transform.anchoredPosition = targetPosition;
                }
            }
        }

        public void EditPaperCallback(bool editing)
        {
            m_Editing = editing;
        }

        public void PaintSizeChangeCallback(float value)
        {
            m_PaintView.ShowLiveTime(value);
            m_ZoomView.ZoomValue = Mathf.Sqrt((100 - value) / 50.0f + 1);
        }
        #endregion
    }
}
